package com.doradance.account.utils;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;

import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;


/**
 * Created by Administrator on 2016/6/28.
 */
public class FragmentUtils {

    public static void backStack(FragmentActivity activity) {
        FragmentManager manager = activity.getSupportFragmentManager();
        manager.popBackStack();
    }

    /**
     * @param fragmentManager 布局管理器
     * @param res             需要添加的viewGroup
     * @param fragment        需要添加的fragment
     */
    public static void addFragment(FragmentManager fragmentManager, @IdRes int res, Fragment fragment) {
        fragmentManager
                .beginTransaction()
                .add(res, fragment)
                .addToBackStack(fragment.getClass().getName())
                .commit();
    }

    /**
     * @param fragmentManager 布局管理器
     * @param res             需要添加的viewGroup
     * @param fragment        需要添加的fragment
     */
    public static void addFirstFragment(FragmentManager fragmentManager, @IdRes int res, Fragment fragment) {
        fragmentManager
                .beginTransaction()
                .add(res, fragment)
                .commit();
    }

    /**
     * 切换fragment，若是在顶部显示 尽量使用replace
     *
     * @param fragmentManager
     * @param res
     * @param fragment
     */
    public static void replaceFragment(FragmentManager fragmentManager, @IdRes int res, Fragment fragment) {
        fragmentManager.beginTransaction()
                .replace(res, fragment)
                .commit();
    }

    /**
     * 切换fragment，若是在顶部显示 尽量使用replace
     * 无动画
     * @param fragmentManager
     * @param res
     * @param fragment
     */
    public static void replaceFragmentNoAnimation(FragmentManager fragmentManager, @IdRes int res, Fragment fragment) {
        fragmentManager.beginTransaction()
                .replace(res, fragment)
                .commit();
    }

    /**
     * 错误提示AlertDialog
     */
    public static void showWarnDialog(String message, FragmentManager manager) {
        final AlertDialogFragment alertDialogFrag = new AlertDialogFragment();
        alertDialogFrag.setMessage(message)
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialogFrag.dismiss();
                    }
                })
                .show(manager);
    }
}
